package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUpMoneyUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.gcgl.domain.GcFk;
import com.ruoyi.gcgl.domain.GcFkitem;
import com.ruoyi.gcgl.domain.GcProjectSqkp;
import com.ruoyi.gcgl.mapper.GcSendTipMapper;
import com.ruoyi.gcgl.service.IGcFkService;
import com.ruoyi.gcgl.service.IGcFkitemService;
import com.ruoyi.gcgl.service.IGcProjectFlowService;
import com.ruoyi.gcgl.service.IGcProjectSqkpService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.web.controller.tool.AliyunSmsUtils;

@Controller
@RequestMapping("/gcgl/fgldsp")
public class GcFgldspController extends BaseController{

	private String prefix = "gcgl/fgldsp";
	
	@Autowired
    private IGcFkService gcFkService;
    @Autowired
    private IGcFkitemService gcFkitemService;
	@Autowired
    private IGcSendTipService gcSendTipService;
	@Autowired
    private IGcProjectSqkpService gcProjectSqkpService;
    @Autowired
    private IGcProjectFlowService gcProjectFlowService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private GcSendTipMapper gcSendTipMapper;
	
	/**
     * 总经理审批
     * @return
     */
    @GetMapping("/ps")
    public String fkps(){
        return prefix + "/fkps";
    }
    
    /**
     * 查询设备材料合同出入库单列表
     */
    @PostMapping("/pslist")
    @ResponseBody
    public TableDataInfo pslist(GcFk gcFk){
        startPage();
        gcFk.setDeleteFlag(1);
        gcFk.setStatus(0);
        gcFk.setSqr(getLoginName());
        List<GcFk> list = gcFkService.selectGcFkList(gcFk);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/yes")
    @ResponseBody
    public AjaxResult yes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcFk gcFk = gcFkService.selectGcFkById(Long.valueOf(id[i].toString()));
    		if(gcFk!=null){
    			GcFk obj = new GcFk();
    			obj.setId(Long.parseLong(id[i]));
    			obj.setStatus(1);
    			obj.setSqrAt(new Date());
    			obj.setSpyj(spyj);
    			gcFkService.updateGcFk(obj);
    			
    			GcFk l_obj = gcFkService.selectGcFkById(Long.parseLong(id[i]));
    			//发送消息通知
//            	gcSendTipService.sendTipXm("fk", "款项支付", l_obj.getOrderNo(), 
//            			l_obj.getCreateUser(), DateUtils.dateTime(l_obj.getCreateAt()), 
//            			l_obj.getStatus().toString(), null, l_obj.getId().toString());
    			
    			try {
            		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(l_obj.getCreateUser());
            		if(userlis!=null && userlis.size()>0){
            			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
            			AliyunSmsUtils.returnSmsTz(phonenumber);
            		}
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/no")
    @ResponseBody
    public AjaxResult no(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcFk gcFk = gcFkService.selectGcFkById(Long.valueOf(id[i].toString()));
    		if(gcFk!=null){
    			GcFk obj = new GcFk();
    			obj.setId(Long.parseLong(id[i]));
    			obj.setStatus(2);
    			obj.setSqrAt(new Date());
    			obj.setSpyj(spyj);
    			gcFkService.updateGcFk(obj);
    			
    			GcFk l_obj = gcFkService.selectGcFkById(Long.parseLong(id[i]));
    			//发送消息通知
//            	gcSendTipService.sendTipXm("fk", "款项支付", l_obj.getOrderNo(), 
//            			l_obj.getCreateUser(), DateUtils.dateTime(l_obj.getCreateAt()), 
//            			l_obj.getStatus().toString(), null, l_obj.getId().toString());
    			
    			try {
            		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(l_obj.getCreateUser());
            		if(userlis!=null && userlis.size()>0){
            			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
            			AliyunSmsUtils.returnSmsTz(phonenumber);
            		}
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @GetMapping("/fksqdzjlsp/{id}")
    public String fksqdzjlsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcFk obj = gcFkService.selectGcFkById(id);
        
    	double ljzfLast =0.0;
    	GcFkitem items = new GcFkitem();
        items.setFkId(id);
        List<GcFkitem> itemlis= gcFkitemService.selectGcFkitemList(items);
        
        if(itemlis!=null && itemlis.size()<3){
        	
        	for(GcFkitem item:itemlis){
        		ljzfLast = DecimalCalculateUtil.add(ljzfLast, 
        				Double.parseDouble(item.getJine().toString()));
            }
        	
        	for(int i=itemlis.size();i<3;i++){
        		GcFkitem itemobj = new GcFkitem();
        		itemobj.setXuh(i);
            	itemlis.add(itemobj);
        	}
        	
        	obj.setItems(itemlis);
        }
//        double ljzf = DecimalCalculateUtil.add(ljzfLast, Double.parseDouble(obj.getLjzf().toString()));
//        obj.setLjzf(Double.valueOf(ljzfLast));
        
        GcFk gcFk11 = new GcFk();
        gcFk11.setDeleteFlag(1);
        gcFk11.setProjectNo(obj.getProjectNo());
        gcFk11.setXmtype(obj.getXmtype());
        gcFk11.setCkdw(obj.getCkdw());
        List<GcFk> gcfklis = gcFkService.selectGcFkList(gcFk11);
        
        double ljzf = 0.0;
        for(GcFk gcFk22:gcfklis){
        	ljzf = DecimalCalculateUtil.add(ljzf, Double.parseDouble(gcFk22.getLjzf().toString()));
        }
        mmap.put("kucljzf", ljzf);
        
        Double h_zbs = DecimalCalculateUtil.div(ljzf, Double.parseDouble(obj.getHtzje().toString()), 4);
    	obj.setZbs(DecimalCalculateUtil.mul(h_zbs, 100)+"%");
        mmap.put("obj", obj);
        mmap.put("riq", DateUtils.dateTime(obj.getRiq()));
        mmap.put("ljzfdx", ConvertUpMoneyUtil.toChinese(obj.getLjzf().toString()));
        return prefix + "/fksqdzjlsp";
    }
    
    //--------------------
    /**
     * 申请开票评审
     * @return
     */
    @GetMapping("/sqkpps")
    public String sqkpps(){
        return prefix + "/sqkpps";
    }
    
    /**
     * 查询申请开票评审列表
     */
    @PostMapping("/sqkppslist")
    @ResponseBody
    public TableDataInfo sqkppslist(GcProjectSqkp gcProjectSqkp)
    {
        startPage();
        gcProjectSqkp.setDeleteFlag(1);
        gcProjectSqkp.setStatus(0);
        gcProjectSqkp.setSqrpr(getLoginName());
        List<GcProjectSqkp> list = gcProjectSqkpService.selectGcProjectSqkpList(gcProjectSqkp);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/sqkpyes")
    @ResponseBody
    public AjaxResult sqkpyes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			GcProjectSqkp gcProjectSqkp = new GcProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(1);
    			gcProjectSqkp.setSqrpr(getLoginName());
    			gcProjectSqkp.setSqrprAt(new Date());
    			gcProjectSqkp.setSpyj(spyj);
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    			
    			GcProjectSqkp l_obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i]));
    			//发送消息通知
//            	gcSendTipService.sendTipXm("sqkp", "申请开票", l_obj.getOrderNo(), 
//            			l_obj.getCreateUser(), DateUtils.dateTime(l_obj.getCreateAt()), 
//            			l_obj.getStatus().toString(), null, l_obj.getId().toString());
    			
    			try {
            		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(l_obj.getCreateUser());
            		if(userlis!=null && userlis.size()>0){
            			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
            			AliyunSmsUtils.returnSmsTz(phonenumber);
            		}
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/sqkpno")
    @ResponseBody
    public AjaxResult sqkpno(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			GcProjectSqkp gcProjectSqkp = new GcProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(2);
    			gcProjectSqkp.setSqrpr(getLoginName());
    			gcProjectSqkp.setSqrprAt(new Date());
    			gcProjectSqkp.setSpyj(spyj);
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
            	
            	GcProjectSqkp l_obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i]));
    			//发送消息通知
//            	gcSendTipService.sendTipXm("sqkp", "申请开票", l_obj.getOrderNo(), 
//            			l_obj.getCreateUser(), DateUtils.dateTime(l_obj.getCreateAt()), 
//            			l_obj.getStatus().toString(), null, l_obj.getId().toString());
            	
            	try {
            		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(l_obj.getCreateUser());
            		if(userlis!=null && userlis.size()>0){
            			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
            			AliyunSmsUtils.returnSmsTz(phonenumber);
            		}
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	}
    	return toAjax(1);
    }
    
    @GetMapping("/sqkpzjlsp/{id}")
    public String sqkpzjlsp(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectSqkp gcProjectSqkp = gcProjectSqkpService.selectGcProjectSqkpById(id);
        
        String projectType = dictDataService.selectDictLabel("gcgl_project_type", gcProjectSqkp.getXmlx());
        String fpType = dictDataService.selectDictLabel("gcgl_project_fp", gcProjectSqkp.getFplx());
        mmap.put("projectType", projectType);
        mmap.put("fpType", fpType);
        
        GcProjectSqkp sqkp = new GcProjectSqkp();
        sqkp.setGcProjectid(gcProjectSqkp.getGcProjectid());
        sqkp.setDeleteFlag(1);
        List<GcProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
        
        double heje = 0.0;
        if(sqkplis!=null && sqkplis.size()<7){
        	
        	for(GcProjectSqkp obj:sqkplis){
        		heje = DecimalCalculateUtil.add(heje, Double.valueOf(obj.getItemje().toString()));
        	}
        	
        	for(int i=sqkplis.size();i<=7;i++){
        		GcProjectSqkp obj = new GcProjectSqkp();
        		sqkplis.add(obj);
        	}
        }
        mmap.put("sqkplis", sqkplis);
        gcProjectSqkp.setHej(heje);
        mmap.put("gcProjectSqkp", gcProjectSqkp);
        return prefix + "/sqkpzjlsp";
    }
}